/*
 * StringUtils.cpp
 *
 *  Created on: 01/05/2012
 *      Author: alex
 */

#include "StringUtils.h"
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cctype>
#include <locale>

using namespace std;

std::vector<std::string> StringUtils::splitString(std::string sentence, char delim)
{
	vector<string> tokens;

    std::stringstream ss(sentence);
    std::string item;

    //Pido el texto hasta cada delimitador y lo inserto en el
    //vector de tokens
    while(std::getline(ss, item, delim))
    	tokens.push_back(item);

	return tokens;
}

std::string StringUtils::itos(int n){
    return StringUtils::NumberToString(n);
}

std::string StringUtils::btos(bool b){
    return (b)? "true" : "false";
}

std::string StringUtils::toLower(std::string str) {
	std::string result("");
	for(unsigned int i=0; i < str.length(); i++ ) {
		result += tolower(str[i]);
	}
	return result;
}

bool StringUtils::equals(std::string str1, std::string str2, bool ignoreCase) {
	if (ignoreCase)
		return StringUtils::toLower(str1).compare(StringUtils::toLower(str2)) == 0;
	else
		return str1.compare(str2) == 0;
}

std::string& StringUtils::ltrim( std::string& s )
{
    s.erase( s.begin(),
            std::find_if( s.begin(), s.end(),
                    std::not1( std::ptr_fun<int, int>( std::isspace ) ) ) );
    return s;
}

std::string& StringUtils::rtrim( std::string& s )
{
    s.erase(
            std::find_if( s.rbegin(), s.rend(),
                    std::not1( std::ptr_fun<int, int>( std::isspace ) ) ).base(),
            s.end() );
    return s;
}

std::string& StringUtils::trim( std::string& s )
{
    return ltrim( rtrim( s ) );
}
